<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <title>点击复制功能的实现</title>
</head>
<body>
<textarea name="text" id="text" cols="30" rows="10" >fdsfasdfsdfa</textarea>
<div class="button"><button onclick="myCopy()">点击复制</button></div>
<input type="text">
<script>
    function myCopy(){
        var ele = document.getElementById("text");//ele是要复制的元素的对象
        ele.focus();
        // ele.select();
        ele.setSelectionRange(0, ele.value.length);
        if(document.execCommand('copy', false, null)){
            //success info
            alert("复制成功")
        } else{
            //fail info
            alert("复制失败")
        }
    }
</script>
</body>
</html>